package com.googlecode.spreadit.matching;

import com.googlecode.spreadit.bean.Message;

/**
 * Objects of this class model a boolean expression of two
 * {@link MatchExpression}s.
 * 
 * @see Relation
 * @author Markus L. Dechert (markus.dechert@googlemail.com)
 */
public class MatchTwo extends MatchExpression {

	private static final long serialVersionUID = -2212010608205237414L;

	private MatchExpression expr1, expr2;

	private Relation operation;

	/**
	 * The fields must be initialized, use {@link #MatchTwo}.
	 */
	private MatchTwo() {
	}

	public MatchTwo(MatchExpression expr1, MatchExpression expr2,
			Relation operation) {
		this();
		this.expr1 = expr1;
		this.expr2 = expr2;
		this.operation = operation;
	}

	@Override
	public boolean matches(Message m) {
		switch (operation) {
		case AND:
			return expr1.matches(m) && expr2.matches(m);
		case OR:
			return expr1.matches(m) || expr2.matches(m);
		case EITHER_OR:
			return expr1.matches(m) ^ expr2.matches(m);
		default:
			throw new RuntimeException(getClass().getSimpleName()
					+ ": not implemented!");
		}
	}

	public String toString() {
		return "( " + expr1 + " " + operation + " " + expr2 + " )";
	}

}
